package com.ksxy.afingerprint.ui.activity

import android.annotation.TargetApi
import android.hardware.fingerprint.FingerprintManager
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.ksxy.afingerprint.R
import com.ksxy.afingerprint.core.KeyStoreHelper
import com.ksxy.afingerprint.ui.activity.base.AbstractReturnActionActivity
import com.ksxy.afingerprint.ui.dialog.FingerPrintAuthRequestDialog
import com.ksxy.afingerprint.utils.CryptUtils
import com.ksxy.afingerprint.utils.showShortToast
import kotlinx.android.synthetic.main.activity_encrypt_or_decrypt.*
import java.security.Key

class EncryptOrDecryptActivity : AbstractReturnActionActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }

    private fun initView() {
        btn_encrypt.setOnClickListener(this)
        btn_decrypt.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.btn_encrypt -> {
                start_encrypt()
            }
            R.id.btn_decrypt -> {
                start_decrypt()
            }
        }
    }

    private fun start_encrypt() {
        val rawData = edt_raw_data.text.toString().trim()
        if (TextUtils.isEmpty(rawData)){
            showShortToast(getString(R.string.unsupport_encrypt_null_str))
            return
        }
        val key = getKey() ?: return
        val cipher = CryptUtils.getEncryptCipherUseKey(key)
        val dialog = FingerPrintAuthRequestDialog()
        dialog.setCipher(cipher)
                .setAuthCallBack(object: FingerPrintAuthRequestDialog.CallBack{
                    override fun onAuthenticationError() {

                    }

                    @TargetApi(Build.VERSION_CODES.M)
                    override fun onAuthenticationSucceeded(result: FingerprintManager.AuthenticationResult?) {
                        val encryptStr = CryptUtils.encrypt2Base64Str(result?.cryptoObject?.cipher!!, rawData)
                        edt_encrypt_data.setText(encryptStr)
                    }
                })
        dialog.show(supportFragmentManager, FingerPrintAuthRequestDialog.TAG)
    }

    private fun start_decrypt() {
        val encrypt_data = edt_encrypt_data.text.toString().trim()
        if (TextUtils.isEmpty(encrypt_data)){
            showShortToast(getString(R.string.unsupport_decrypt_null_str))
            return
        }
        val key = getKey() ?: return
        val cipher = CryptUtils.getDecryptCipherUseKey(key)
        val dialog = FingerPrintAuthRequestDialog()
        dialog.setCipher(cipher)
                .setAuthCallBack(object: FingerPrintAuthRequestDialog.CallBack{
                    override fun onAuthenticationError() {

                    }

                    @TargetApi(Build.VERSION_CODES.M)
                    override fun onAuthenticationSucceeded(result: FingerprintManager.AuthenticationResult?) {
                        val rawStr = CryptUtils.decryptFormBase64Str(result?.cryptoObject?.cipher!!, encrypt_data)
                        edt_raw_data.setText(rawStr)
                    }
                })
        dialog.show(supportFragmentManager, FingerPrintAuthRequestDialog.TAG)
    }

    private fun getKey(): Key?{
        val keyAlais = edt_key_alias.text.toString().trim()
        if(TextUtils.isEmpty(keyAlais)){
            edt_key_alias_wrapper.error = getString(R.string.no_null)
            return null
        }
        if(!KeyStoreHelper.isExistedKey(keyAlais)){
            edt_key_alias_wrapper.error = getString(R.string.key_not_existed)
            return null
        }
        edt_key_alias_wrapper.error = ""
        return KeyStoreHelper.getKeyByAlais(keyAlais)
    }

    override fun getLayoutResId(): Int {
        return R.layout.activity_encrypt_or_decrypt
    }

}
